package com.tim.app.ui.adapter.found;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.log.DLOG;
import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.server.entry.Special;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 专题的适配器
 */
public class SpecialItemAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Special> {

    static final String TAG = "SpecialItemAdapter";

    private Context mContext;

    public SpecialItemAdapter(Context mContext, List<Special> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_special, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder mHolder, int position, final Special data) {

        if (null == data)
            return;
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getImage())) {
            BitmapLoader.ins().loadImage(data.getImage(), holder.rivImage);
        } else {
            BitmapLoader.ins().loadImage("", holder.rivImage);
        }
        if (!TextUtils.isEmpty(data.getName())) {
            holder.tvSpecialName.setText("#" + data.getName() + "#");
        } else {
            holder.tvSpecialName.setText("");
        }

        holder.tvLookAtNumber.setText(mContext.getString(R.string.lookAtNumber, String.valueOf(data.getOnlookersSum() + data.getSysOnlookersSum())));
    }


    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivImage;
        TextView tvSpecialName;
        TextView tvLookAtNumber;

        public ViewHolder(View itemView) {
            super(itemView);
            rivImage = (RoundedImageView) itemView.findViewById(R.id.rivImage);
            tvSpecialName = (TextView) itemView.findViewById(R.id.tvSpecialName);
            tvLookAtNumber = (TextView) itemView.findViewById(R.id.tvLookAtNumber);
            RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, (int) ((RT.getScreenWidth() - (int) (RT.getDensity() * 28)) * 0.501));
            itemView.setLayoutParams(params);
        }

    }

}
